package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.PengYouQuanInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrater on 2015/12/28.
 */
public class RiJiAdapter extends BaseAdapter {
    private Context mContext;
    private List<PengYouQuanInfo> mList = new ArrayList<PengYouQuanInfo>();

    public RiJiAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<PengYouQuanInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.riji_item, null);
            childHolder.mDay = (TextView) convertView.findViewById(R.id.day);
            childHolder.mMonth = (TextView) convertView.findViewById(R.id.month);
            childHolder.mLogo = (ImageView) convertView.findViewById(R.id.riji_logo);
            childHolder.mTitle = (TextView) convertView.findViewById(R.id.riji_title);
            childHolder.mNumber = (TextView) convertView.findViewById(R.id.riji_shu);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

//        AreaInfo cinfo = mList.get(position);
//        if (cinfo != null) {
//            childHolder.cityName.setText(cinfo.getAreaName());
//
//        }
        PengYouQuanInfo info = mList.get(position);
        if(info!=null){
            String ss = info.getDairyImage();
            if(!TextUtils.isEmpty(ss)){
                List<String> apdlist = Arrays.asList(ss.split(";"));
                if(apdlist!=null && apdlist.size()>0){
                    String headImgUrl = apdlist.get(0);
                    if(headImgUrl != null && headImgUrl.length()>0){
                        ViewUtil.Size size =  ViewUtil.getHeadImageSize_40(mContext);
                        Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(headImgUrl,size));
                        if (cacheBitmap != null){
                            childHolder.mLogo.setImageBitmap(cacheBitmap);
                        }else {
                            childHolder.mLogo.setImageResource(R.drawable.default_head_image);
                            ImageCache.getInstance(mContext).loadBitmapToView(childHolder.mLogo, headImgUrl, size);
                        }

                    }else{
                        childHolder.mLogo.setImageResource(R.drawable.default_head_image);
                    }
                    childHolder.mNumber.setText("共"+info.getImageCount()+"张");
                }


            }else {
                childHolder.mLogo.setVisibility(View.GONE);
                childHolder.mNumber.setVisibility(View.GONE);
            }


            if(info.getDairyContent()!=null){
                try {
                    childHolder.mTitle.setText(URLDecoder.decode(info.getDairyContent(),"utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            String allDate = info.getDairyTime();
            String days = allDate.substring(0,3);
            String mon = allDate.substring(allDate.length()-2,allDate.length());
            childHolder.mDay.setText(mon);
            childHolder.mMonth.setText(days);
        }

        return convertView;
    }

    class ChildHolder {
        TextView mDay;
        TextView mMonth;
        ImageView mLogo;
        TextView mTitle;
        TextView mNumber;
    }
}